package com.bm.specialvehiclefordispatcher.activity.mine;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.bm.corelibs.logic.BaseLogic.NListener;
import com.bm.corelibs.utils.ToastMgr;
import com.bm.specialvehiclefordispatcher.R;
import com.bm.specialvehiclefordispatcher.activity.BaseActivity;
import com.bm.specialvehiclefordispatcher.activity.main.MainOrdersFragment;
import com.bm.specialvehiclefordispatcher.bean.BaseData;
import com.bm.specialvehiclefordispatcher.constants.Constant;
import com.bm.specialvehiclefordispatcher.logics.LoginManager;
import com.bm.specialvehiclefordispatcher.logics.OrderManager;
import com.bm.specialvehiclefordispatcher.views.CommonNavigationBar;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;

public class MyOrderActivity extends BaseActivity implements OnPageChangeListener, OnClickListener {
	
	private CommonNavigationBar navi;
	private ViewPager container;
	private TextView tv_doing, tv_done, tv_all;
	private View selector;
	
	private MainOrdersFragment doingFragment, doneFragment, allFragment;
	private OrderManager doingManager = new OrderManager();
	private OrderManager doneManager = new OrderManager();
	private OrderManager allManager = new OrderManager();
	
	/** 导航色块宽度 **/
	private int selectorWidth;
	private String dispatcherId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_myorder);
		findViews();
		init();
		addListeners();
	}

	@Override
	public void findViews() {
		navi = (CommonNavigationBar) findViewById(R.id.navi);
		container = (ViewPager) findViewById(R.id.container);
		tv_doing = (TextView) findViewById(R.id.tv_doing);
		tv_done = (TextView) findViewById(R.id.tv_done);
		tv_all = (TextView) findViewById(R.id.tv_all);
		selector = (View) findViewById(R.id.v_divider);
	}

	@Override
	public void init() {
		navi.setTitle(R.string.myorder);
		navi.setListener(null);
		getSelectorWidth();
		container.setOffscreenPageLimit(3);
		container.setAdapter(new MainAdapter(getSupportFragmentManager()));
		dispatcherId = LoginManager.getInstance(mActivity).getId();
		loadingDialog.show();
	}

	@Override
	public void addListeners() {
		container.setOnPageChangeListener(this);
		tv_doing.setOnClickListener(this);
		tv_done.setOnClickListener(this);
		tv_all.setOnClickListener(this);
	}
	
	private void getSelectorWidth() {
		selector.post(new Runnable() {
			@Override
			public void run() {
				selectorWidth = selector.getWidth();
			}
		});
	}
	
	@Override
	public void onPageScrollStateChanged(int state) {}

	@Override
	public void onPageScrolled(int position, float percent, int px) {}

	@Override
	public void onPageSelected(int position) {
		selector.setTranslationX(position * selectorWidth);
		switch (position) {
		case 0:
			tv_doing.setTextColor(getResources().getColor(R.color.main));
			tv_done.setTextColor(getResources().getColor(R.color.main_text));
			tv_all.setTextColor(getResources().getColor(R.color.main_text));
			break;

		case 1:
			tv_doing.setTextColor(getResources().getColor(R.color.main_text));
			tv_done.setTextColor(getResources().getColor(R.color.main));
			tv_all.setTextColor(getResources().getColor(R.color.main_text));
			break;
			
		case 2:
			tv_doing.setTextColor(getResources().getColor(R.color.main_text));
			tv_done.setTextColor(getResources().getColor(R.color.main_text));
			tv_all.setTextColor(getResources().getColor(R.color.main));
			break;
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_doing:
			loadingDialog.show();
			doingFragment.resetData();
			doingManager.getFirst(dispatcherId, 1, getDoingListener());
			container.setCurrentItem(0);
			break;

		case R.id.tv_done:
			loadingDialog.show();
			doneFragment.resetData();
			doneManager.getFirst(dispatcherId, 2, getDoneListener());
			container.setCurrentItem(1);
			break;
			
		case R.id.tv_all:
			loadingDialog.show();
			allFragment.resetData();
			allManager.getFirst(dispatcherId, 3, getAllListener());
			container.setCurrentItem(2);
			break;
		}
	}
	
	class MainAdapter extends FragmentPagerAdapter {

		public MainAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				if (doingFragment == null) {
					doingFragment = new MainOrdersFragment();
					doingFragment.setRefreshListener(doingRefreshListener);
					doingManager.getFirst(dispatcherId, 1, getDoingListener());
				}
				return doingFragment;

			case 1:
				if (doneFragment == null) {
					doneFragment = new MainOrdersFragment();
					doneFragment.setRefreshListener(doneRefreshListener);
					doneManager.getFirst(dispatcherId, 2, getDoneListener());
				}
				return doneFragment;
				
			case 2:
				if (allFragment == null) {
					allFragment = new MainOrdersFragment();
					allFragment.setRefreshListener(allRefreshListener);
					allManager.getFirst(dispatcherId, 3, getAllListener());
				}
				return allFragment;
			}
			return null;
		}
	}
	
	private OnRefreshListener2<ListView> doingRefreshListener = new OnRefreshListener2<ListView>() {
		@Override
		public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
			doingFragment.resetData();
			doingFragment.getRefreshableView().enableLoading();
			doingManager.getFirst(dispatcherId, 1, getDoingListener());
		}

		@Override
		public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
			doingManager.getNext(dispatcherId, 1, getDoingListener());
		}
	};

	private OnRefreshListener2<ListView> doneRefreshListener = new OnRefreshListener2<ListView>() {
		@Override
		public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
			doneFragment.resetData();
			doneFragment.getRefreshableView().enableLoading();
			doneManager.getFirst(dispatcherId, 2, getDoneListener());
		}

		@Override
		public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
			doneManager.getNext(dispatcherId, 2, getDoneListener());
		}
	};
	
	private OnRefreshListener2<ListView> allRefreshListener = new OnRefreshListener2<ListView>() {
		@Override
		public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
			allFragment.resetData();
			allFragment.getRefreshableView().enableLoading();
			allManager.getFirst(dispatcherId, 3, getAllListener());
		}

		@Override
		public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
			allManager.getNext(dispatcherId, 3, getAllListener());
		}
	};

	private NListener<BaseData> getDoingListener() {
		return new NListener<BaseData>() {
			@Override
			public void onResponse(BaseData data) {
				loadingDialog.dismiss();
				doingFragment.onRefreshCompleted();
				if (data.status == Constant.STATUS_OK) {
					if (data.data.DispatcherOrderVOList != null) {
						doingFragment.setData(data.data.DispatcherOrderVOList);
						doingManager.setMaxPage(data.data.page.pageCount);
					}
				} else {
					ToastMgr.show(data.msg);
				}
			}

			@Override
			public void onErrResponse(VolleyError error) {
				doingFragment.onRefreshCompleted();
				loadingDialog.dismiss();
			}

			@Override
			public void onAllPageLoaded(int nowPage, int totalPage) {
				doingFragment.getRefreshableView().disableLoading();
			}
		};
	}

	private NListener<BaseData> getDoneListener() {
		return new NListener<BaseData>() {
			@Override
			public void onResponse(BaseData data) {
				doneFragment.onRefreshCompleted();
				loadingDialog.dismiss();
				if (data.status == Constant.STATUS_OK) {
					if (data.data.DispatcherOrderVOList != null) {
						doneFragment.setData(data.data.DispatcherOrderVOList);
						doneManager.setMaxPage(data.data.page.pageCount);
					}
				} else {
					ToastMgr.show(data.msg);
				}
			}

			@Override
			public void onErrResponse(VolleyError error) {
				doneFragment.onRefreshCompleted();
			}

			@Override
			public void onAllPageLoaded(int nowPage, int totalPage) {
				doneFragment.getRefreshableView().disableLoading();
			}
		};
	}
	
	private NListener<BaseData> getAllListener() {
		return new NListener<BaseData>() {
			@Override
			public void onResponse(BaseData data) {
				allFragment.onRefreshCompleted();
				loadingDialog.dismiss();
				if (data.status == Constant.STATUS_OK) {
					if (data.data.DispatcherOrderVOList != null) {
						allFragment.setData(data.data.DispatcherOrderVOList);
						allManager.setMaxPage(data.data.page.pageCount);
					}
				} else {
					ToastMgr.show(data.msg);
				}
			}

			@Override
			public void onErrResponse(VolleyError error) {
				allFragment.onRefreshCompleted();
			}

			@Override
			public void onAllPageLoaded(int nowPage, int totalPage) {
				allFragment.getRefreshableView().disableLoading();
			}
		};
	}
}
